package com.jeecms.wicool.action.shops;

import static com.jeecms.common.page.SimplePage.cpn;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.manager.main.CmsLogMng;
import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.shops.ServiceUser;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.shops.ServiceUserMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;
import com.jeecms.wicool.manager.user.UserInfoMng;
import com.jeecms.wicool.manager.util.DictionaryMng;

/**
 * ServiceUserAction
 */
@Controller
public class ServiceUserAct {
	private static final Logger log = LoggerFactory.getLogger(ServiceUserAct.class);

	@RequestMapping("/serviceUser/v_listServiceUser.do")
	public String list(String serviceId,String serviceName,
			Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(serviceId, serviceName, 
				cpn(pageNo), CookieUtils.getPageSize(request),CmsUtils.getUser(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("serviceId", serviceId);
		model.addAttribute("serviceName", serviceName);
		return "shops/list_ServiceUser";
	}

	

	@RequestMapping("/serviceUser/v_addServiceUser.do")
	public String add(ModelMap model,HttpServletRequest request) {
		ServiceUser serviceUser= new ServiceUser();
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("serviceUser", serviceUser);
		model.addAttribute("listShop", listShop);
		return "shops/add_ServiceUser";
	}

	@RequestMapping("/serviceUser/v_editServiceUser.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ServiceUser serviceUser = manager.findById(id);
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("serviceUser", serviceUser);
		model.addAttribute("listShop", listShop);
		return "shops/edit_ServiceUser";
	}
	@RequestMapping("/serviceUser/v_viewServiceUser.do")
	public String view(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ServiceUser serviceUser = manager.findById(id);
		model.addAttribute("serviceUser", serviceUser);
		return "shops/view_ServiceUser";
	}

	@RequestMapping("/serviceUser/o_saveServiceUser.do")
	public String save(ServiceUser bean,Integer shopId, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(bean, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = manager.save(bean, shopId);
		log.info("save ServiceUser id={}", bean.getId());
		return "redirect:v_listServiceUser.do";
	}

	@RequestMapping("/serviceUser/o_updateServiceUser.do")
	public String update(ServiceUser bean, Integer shopId,String serviceId,String serviceName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateUpdate(bean.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = manager.update(bean, shopId);
		model.addAttribute("message", "global.success");
		log.info("update ServiceUser success. id={}", bean.getId());
		return list(serviceId,serviceName, pageNo, request, model);
	}

	@RequestMapping("/serviceUser/o_deleteServiceUser.do")
	public String delete(Integer[] ids, String serviceId,String serviceName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ServiceUser[] beans = manager.deleteByIds(ids);
		for (ServiceUser bean : beans) {
			log.info("delete ServiceUser id={}", bean.getId());
		}
		return list(serviceId, serviceName, pageNo, request, model);
	}

	private WebErrors validateSave(ServiceUser bean, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		return errors;
	}

	private WebErrors validateEdit(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		// TODO 验证是否为管理员，管理员不允许修改。
		return errors;
	}

	private WebErrors validateDelete(Integer[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		return errors;
	}

	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		ServiceUser entity = manager.findById(id);
		if (errors.ifNotExist(entity, ServiceUser.class, id)) {
			return true;
		}
		return false;
	}

	@Autowired
	private ServiceUserMng manager;
	@Autowired
	private CmsLogMng cmsLogMng;
	@Autowired
	private UserInfoMng userMng;
	@Autowired
	private DictionaryMng DicMng;
	@Autowired
	private WiShopInfoMng shopMng;
}
